<?php

/* 
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

namespace backend\controllers;


use bc\controller\BackendController;
use bc\model\ProductJpgModel;
use bc\model\ProductModel;
use yii;

class ProductJpgController extends BackendController
{
    public function actionIndex(){
        $id = $this->get('id');
        $productM = new ProductModel();
        $productD = $productM->getRowById(['product_id'=>$id]);
        $view['product_id'] = $productD;
        return $this->render('index',$view);
    }

    public function actionUpload(){
        $product_id = $this->get("product_id");
        if(empty($product_id)) return "失败，请刷新重试";
        $images_name = array();
        $img_name = time().rand(10000, 99999);
        if (isset($_POST)) {
            foreach ($_FILES['image_data']['tmp_name'] as $k => $v) {
                move_uploaded_file($v, UPLOAD_PATH .'/upload/product_jpg/'. $img_name . $k . '.jpg');
                $images_name[]= $img_name . $k . '.jpg';
            }
        }else{
            return "失败！";
        }

        $productjpgM = new ProductJpgModel();
        //删除以前的图片
        $productjpgM->deleteAll(['product_id'=>$product_id]);
        foreach($images_name as $k => $v)
        {
            $productjpgMadd = new ProductJpgModel();
            $data=array(
                'product_id' => $product_id,
                'jpg_imgurl' => $v,
                'jpg_sort' => $k+1,
                'create_time' => time(),
                'upate_time' => time()
            );

            $productjpgMadd->add($data);
            unset($data);
            unset($productjpgMadd);
        }
        return 1;
    }

}